// 
//  NewNoteCommand.cs
//  
//  Author:
//       Ray Shaw <demogodyou@gmail.com>
// 
//  Copyright (c) 2009 Ray Shaw(xiaolei)
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:2.0.50727.3053
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using SharpNote.Core.Command;
using SharpNote.Core.Entity;
using SharpNote.Core.Config;
using SharpNote.Core.Data;

namespace SharpNote.Core.Command
{
	/// <summary>
	/// Create new note. 
	/// </summary>
	[CommandAttribute("NewNote", "note,new,+note,nnote,cnote,cn,n")]
	public class NewNoteCommand : CommandBase
	{
		
		public NewNoteCommand ()
		{
		}
		
		public override void Execute(ref object result, params object[] parameters)
		{
		    if(parameters == null)
			{
				Console.WriteLine(GetHelpeText());
				
				result = false;
				return;
			}
			result = true;
			NoteInfo noteInfo = null; 
				
			if(parameters.Length == 0)
			{
				Console.WriteLine(GetHelpeText());
				return;
			}
			if(parameters.Length == 1)
			{
			    string content = parameters[0].ToString();
				
				if(content.Equals("/?", StringComparison.CurrentCultureIgnoreCase)
				   || content.Equals("/help", StringComparison.CurrentCultureIgnoreCase))
				{
					Console.WriteLine(GetHelpeText());
					return;
				}
				
				noteInfo = new NoteInfo();
				noteInfo.Title = string.Empty;
				noteInfo.Content = content;
				noteInfo.Id = Guid.NewGuid().ToString();
				noteInfo.Encrypt(SysConfig.GetInstance().PrivateKeyPlainText);
				
				result = DbObjectMappingHelper.Create<NoteInfo>(noteInfo);
			}else if(parameters.Length == 2)
			{
			    string title = parameters[0].ToString();
				string content = parameters[1].ToString();
				
				noteInfo = new NoteInfo();
				noteInfo.Title = title;
				noteInfo.Content = content;
				noteInfo.Id = Guid.NewGuid().ToString();
				noteInfo.Encrypt(SysConfig.GetInstance().PrivateKeyPlainText);
				
				result = DbObjectMappingHelper.Create<NoteInfo>(noteInfo);
			}else if (parameters.Length == 3)
			{
			    string title = parameters[0].ToString();
				string content = parameters[1].ToString();
				string tags = parameters[2].ToString();
				
				noteInfo = new NoteInfo();
				noteInfo.Title = title;
				noteInfo.Content = content;
				noteInfo.Tags = tags;
				noteInfo.Id = Guid.NewGuid().ToString();
				noteInfo.Encrypt(SysConfig.GetInstance().PrivateKeyPlainText);	
				
				result = DbObjectMappingHelper.Create<NoteInfo>(noteInfo);
			}else
			{
			    Console.WriteLine("Invalid arguments.");
				Console.WriteLine(GetHelpeText());
				
				return;
			}
			
			if(Convert.ToBoolean(result))
			{
				Console.WriteLine(string.Format(">>Create note success. Note id is {0}.", noteInfo.Id));
			}else
			{
				Console.WriteLine(">>Create note failed.");
			}
		}
		
		public string GetHelpeText()
		{
			return "\nCommand: \n  NewNote [title] content [tags]\nSummary: \n  Create a new note.\nAlias: \n  nnote,new,+note,nnote,cnote,cn,n\nParameters: \n  title: title of the note.\n  content: content of the note. \n  tags: tags of the note.(sample: personal,family)\n";
		}
		
		public string GetAboutText()
		{
			return "\nCreated by Ray Shaw(demogodyou@gmail.com.";
		}
	}
}
